﻿using LBSoft.Business;
using LBSoft.Common.DotNetBean;
using LBSoft.Common.DotNetCode;
using LBSoft.Common.DotNetData;
using LBSoft.Common.DotNetExt;
using LBSoft.Common.DotNetJson;
using LBSoft.Common.DotNetUI;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace LBSoft.HplusAdmin.Business.ButtonManage
{
    /// <summary>
    /// ButtonManage 的摘要说明
    /// </summary>
    public class ButtonManage : IHttpHandler
    {
        HttpContext con = null;
        mJsonResult json = new mJsonResult();
        Common_Dal systemidao = new Common_Dal();
        LogHelper log = new LogHelper();

        public void ProcessRequest(HttpContext context)
        {
            con = context;

            switch (con.Request["PostType"])
            {
                case "list": LoadList(); break;
                case "get": GetData(); break;
                case "set": SetData(); break;
                case "del": DelData(); break;
            }
        }

        #region 列表
        private void LoadList()
        {
            switch (con.Request["ActionType"])
            {
                case "Button": LoadButtonList(); break; //按钮列表
            }
        }

        /// <summary>
        /// 按钮列表
        /// </summary>
        private void LoadButtonList()
        {
            int count = 0;
            int pageSize = con.Request["rows"].ToInt32(15);
            int curPage = con.Request["page"].ToInt32(1);

            IList<SqlParam> IList_param = new List<SqlParam>();
            StringBuilder SqlWhere = new StringBuilder(WebHelper.GetCondition(con.Request));
            SqlWhere.AppendFormat(" AND IsDelete=0");

            string tables = @" SYS_Button ";
            string cols = @" * ";
            string orderStr = " Sort Asc ";

            DataTable dt = systemidao.GetListByPage(tables, cols, SqlWhere, orderStr, IList_param, curPage, pageSize, ref count);

            json.success = true;
            json.total = count;
            json.rows = dt;

            con.Response.Write(JsonConvert.SerializeObject(json));
        }
        #endregion

        #region 获取
        private void GetData()
        {
            switch (con.Request["ActionType"])
            {
                case "Button": GetButton(); break; //获取按钮
            }
        }

        /// <summary>
        /// 获取按钮
        /// </summary>
        private void GetButton()
        {
            string guid = con.Request["GUID"].ToString2();

            Hashtable ht = systemidao.GetDataByID("SYS_Button", "GUID", guid);

            con.Response.Write(JsonConvert.SerializeObject(ht));
        }
        #endregion

        #region  编辑
        private void SetData()
        {
            switch (con.Request["ActionType"])
            {
                case "Button": SetButton(); break; //按钮保存
            }
        }

        /// <summary>
        /// 按钮保存
        /// </summary>
        private void SetButton()
        {
            try
            {
                string userId = RequestSession.GetSessionUser().UserId;
                string userAccount = RequestSession.GetSessionUser().UserAccount;

                string guid = con.Request["GUID"].ToString2();
                string buttonName = con.Request["ButtonName"].ToString2();

                DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder($@"SELECT GUID FROM dbo.SYS_Button WHERE IsDelete=0 AND ButtonName='{buttonName}' AND GUID!='{guid}'"));
                if (dt != null && dt.Rows.Count > 0)
                {
                    json.msg = "按钮名称已存在！";
                    con.Response.Write(JsonConvert.SerializeObject(json));
                    return;
                }

                Hashtable ht = systemidao.AddHashtable("SYS_Button", con);

                if (string.IsNullOrEmpty(guid))
                {
                    ht["IsDelete"] = 0;
                    ht["CreateDate"] = DateTime.Now;
                    ht["CreateUserId"] = userId;
                    ht["CreateAccount"] = userAccount;
                }
                else
                {
                    ht.Remove("IsDelete");
                    ht.Remove("CreateDate");
                    ht.Remove("CreateUserId");
                    ht.Remove("CreateAccount");
                    ht["ModifyDate"] = DateTime.Now;
                    ht["ModifyUserId"] = userId;
                    ht["ModifyAccount"] = userAccount;
                }

                bool result = systemidao.SubmitData("SYS_Button", "GUID", guid, ht);
                json.success = result;

                if (result)
                {
                    json.msg = "保存成功！";
                }
                else
                {
                    json.msg = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                json.msg = ex.Message;
                log.WriteExceptionLog(ex);
            }

            con.Response.Write(JsonConvert.SerializeObject(json));
        }
        #endregion

        #region 删除
        private void DelData()
        {
            switch (con.Request["ActionType"])
            {
                case "Button": DelButton(); break; //按钮删除
            }
        }

        /// <summary>
        /// 按钮删除
        /// </summary>
        private void DelButton()
        {
            string guid = con.Request["GUID"].ToString2();

            if (string.IsNullOrEmpty(guid))
            {
                json.success = false;
                json.msg = "没有要删除的主键！";
                con.Response.Write(JsonConvert.SerializeObject(json));
                return;
            }

            List<string> list = guid.Split(',').ToList<string>();
            bool result = systemidao.BatchUpdateDeleteData("SYS_Button", "GUID", list.ToArray()) > 0;
            json.success = result;

            if (result)
            {
                json.msg = "删除成功！";
            }
            else
            {
                json.msg = "删除失败！";
            }

            con.Response.Write(JsonConvert.SerializeObject(json));
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}